;
(function(config) {
	var configName = "config.json";
	window['Dong'] = {
		version : '1.0',
		param : {
			Maxid : 1000,
			Left : "",
			Module : null,
			Imports : [],
			RootPath : null
		},
		getRootPath : function() {
			// 获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
			var curWwwPath = window.document.location.href;
			// 获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
			var pathName = window.document.location.pathname;
			var pos = curWwwPath.indexOf(pathName);
			// 获取主机地址，如： http://localhost:8083
			var localhostPaht = curWwwPath.substring(0, pos);
			// 获取带"/"的项目名，如：/uimcardprj
			var projectName = pathName.substring(0, pathName.substr(1).indexOf(
					'/') + 1);
			return (localhostPaht + projectName);
		},
		getHttpRequest : function() {
			if (window.XMLHttpRequest) {
				var _xmlHttp = new XMLHttpRequest();
				if (_xmlHttp.overrideMimeType) {
					_xmlHttp.overrideMimeType('text/xml');
				}
				return _xmlHttp;
			} else if (window.ActiveXObject) {
				return new ActiveXObject("MsXml2.XmlHttp");
			} else {
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
		},
		getJavaScript : function(_path) {
			var _dong = this, _httpRequest = _dong.getHttpRequest();
			var _text = null;
			_httpRequest.onreadystatechange = function() {
				if (_httpRequest.readyState == 4) {
					if (_httpRequest.status == 200
							|| _httpRequest.status == 304) {
						_text = _httpRequest.responseText;
					} else {
						alert('request error:' + _httpRequest.statusText + ' ('
								+ _httpRequest.status + ')');
						return;
					}
				}
			};
			_httpRequest.open('GET', _path, false);
			_httpRequest.send(null);
			return _text;
		},
		ajaxRequest : function(_key, _url) {
			var _end = _url.substring(_url.lastIndexOf('/') + 1), _dong = this, _httpRequest = _dong
					.getHttpRequest();
			_httpRequest.onreadystatechange = function() {
				if (_httpRequest.readyState == 4) {
					if (_httpRequest.status == 200
							|| _httpRequest.status == 304) {
						var _imports = _dong.param.Imports, state = 0;
						for (var i = 0; i < _imports.length; i++) {
							if (_imports[i] == _key) {
								state++;
								break;
							}
						}
						if (state == 0) {
							if (_end == configName) {
								_dong.param.Module = eval('('
										+ _httpRequest.responseText + ')');
							} else {
								_dong.importJs(_key, _httpRequest.responseText);
							}
						}
					} else {
						alert('request error:' + _httpRequest.statusText + ' ('
								+ _httpRequest.status + ')');
						return;
					}
				}
			};
			_httpRequest.open('GET', _url, false);
			_httpRequest.send(null);
		},
		importJs : function(_key, _text) {
			if (_text != null) {
				var _dong = this;
				var _imports = _dong.param.Imports;
				if (_imports.length == 0) {
					_dong.getDom("script").each(
							function(e) {
								var _srcText = e.getAttribute("src");
								if (!!_srcText) {
									if (_srcText.indexOf('/') != -1) {
										var _end = _srcText.substring(_srcText
												.lastIndexOf("/") + 1);
										if (_end == config.Name) {
											var _script = document
													.createElement("script");
											_script.language = "javascript";
											_script.type = "text/javascript";
											_script.defer = true;
											_script.text = _text;
											_script.id = _key;
											// alert(e.parentNode.lastChild);
											// e.appendChild(_script);
											_dong.appendNode(e, _script);
										}
									}
								}
							});
				} else {
					var _e = document
							.getElementById(_imports[_imports.length - 1]);
					var _script = document.createElement("script");
					_script.language = "javascript";
					_script.type = "text/javascript";
					_script.defer = true;
					_script.text = _text;
					_script.id = _key;
					// _e.appendChild(_script);
					_dong.appendNode(_e, _script);
				}
				_imports[_imports.length] = _key;
			}
		},
		init : function() {
			var _left = this.getRootPath() + "/";
			this.param.RootPath = _left;
			this.ajaxRequest(configName, _left + config.Module);
		},
		// getBasePath : function() {
		// var _script = document.scripts, _name = "/" + config.Name, _len =
		// _name.length;
		// for (var i = 0; i < _script.length; i++) {
		// var _src = _script[i].src;
		// if (_src.substring(_src.length - _len) == _name) {
		// _src = _src.substring(_src.lastIndexOf(':') + 1);
		// var _index = _src.indexOf('/') + 1;
		// _src = _src.substring(_src.indexOf('/', _index) + 1);
		// if (_src.length > config.Name.length) {
		// _src = _src.substring(0, _src.length
		// - config.Name.length);
		// return _src;
		// }
		// }
		// }
		// return "";
		// },
		defined : function(_arr, _fn) {
			var _package = _arr[0];
			var _import = _arr[1];
			if (!!_import) {
				var _left = this.param.Left;
				for (var _a = 0; _a < _import.length; _a++) {
					var _key = _import[_a];
					if (!this.isExist(this, _key)) {
						var _m = this.param.Module[_key];
						this.ajaxRequest(_key, _left + _m.path);
					}
				}
			}
			(function(_p, _d) {
				_fn.call(this, _d, _p);
			})(_package, this);
		},
		setModule : function(_key, _fn) {
			var _right = _key.indexOf('.') == -1 ? _key : _key.substring(_key
					.lastIndexOf('.') + 1);
			this.checkPackage(_key)[_right] = _fn;
		},
		checkPackage : function(_p) {
			var _left = _p.indexOf('.') == -1 ? "" : _p.substring(0, _p
					.lastIndexOf('.'));
			if (!!_left) {
				this.createPackage(this, _left);
				return eval('Dong.' + _left);
			} else {
				return this;
			}
		},
		createPackage : function(_a, _b) {
			var _index = _b.indexOf('.');
			if (_index == -1) {
				var _left = _b;
				if (!_a[_left]) {
					_a[_left] = {};
				}
			} else {
				var _left = _b.substring(0, _index);
				var _right = _b.substring(_index + 1);
				if (!_a[_left]) {
					_a[_left] = {};
				}
				this.createPackage(_a[_left], _right);
			}
		},
		isExist : function(_d, _package) {
			var _index = _package.indexOf('.');
			if (_index == -1) {
				var _left = _package;
				return !!_d[_left];
			} else {
				var _left = _package.substring(0, _index);
				if (!!_d[_left]) {
					var _right = _package.substring(_index + 1);
					return this.isExist(_d[_left], _right);
				} else {
					return false;
				}
			}
		},
		getMid : function() {
			this.param.Maxid++;
			var _maxid = this.param.Maxid + "_Maxid";
			return _maxid;
		},
		remove : function(_key) {
			if (!!_key) {
				if (_key == 'Dong' && !!window[_key]) {
					window[_key] = null;
				} else {
					if (_key.startsWith("Dong.")) {
						var _len = _key.indexOf('.');
						var _left = _key.substring(0, _len);
						if (_key == "Dong.") {
							window[_left] = null;
						} else {
							var _right = _key.substring(_len + 1);
							this.loopRemove(window[_left], _right);
						}
					} else {
						if (!!_key) {
							this.loopRemove(window["Dong"], _key);
						}
					}
				}
			}
		},
		loopRemove : function(_w, _k) {
			if (!!_w && !!_k) {
				var _len = _k.indexOf('.');
				if (_len == -1) {
					if (!!_w[_k]) {
						_w[_k] = null;
					}
				} else {
					var _left = _k.substring(0, _len);
					var _right = _k.substring(_len + 1);
					if (!!_w[_left]) {
						this.loopRemove(_w[_left], _right);
					}
				}
			}
		},
		getDom : function(n) {
			return {
				el : document.getElementsByTagName(n),
				each : function(_fn) {
					var _dom = this.el;
					if (!!_dom && !!_fn) {
						var _len = _dom.length;
						for (var i = 0; i < _len; i++) {
							_fn(_dom[i]);
						}
					}
				}
			};
		},
		appendNode : function(e, node) {
			var parent = e.parentNode;
			var arr = parent.childNodes;
			var len = arr.length;
			var index = 0;
			for (var i = 0; i < len; i++) {
				if (arr[i] == e) {
					index = i;
				}
			}
			var ele = null;
			if (index == len - 1) {
				parent.insertBefore(node);
			} else {
				parent.insertBefore(node, arr[index + 1]);
			}
		}
	};
	Dong.importOnly = function(_packages) {
		this.init();
		var _left = this.param.RootPath;
		var _map = this.param.Module;
		for (var _i = 0; _i < _packages.length; _i++) {
			var _m = _map[_packages[_i]];
			var _key = _packages[_i];
			var exist = this.isExist(this, _key);
			if (!exist) {
				this.ajaxRequest(_key, _left + _m.path);
			}
		}
	};
	Dong.imported = function(_packages, _fn) {
		this.init();
		var _left = this.param.RootPath;
		var _map = this.param.Module;
		var _obj = new Array();
		for (var _i = 0; _i < _packages.length; _i++) {
			var _key = _packages[_i];
			var _m = _map[_key];
			var _name = "Dong.".concat(_key);
			var exist = this.isExist(this, _key);
			if (!!exist) {
				_obj.push(eval(_name));
			} else {
				this.ajaxRequest(_key, _left + _m.path);
				_obj.push(eval(_name));
			}
		}
		if (!!_fn) {
			_fn.apply(this, _obj);
		}
	};
})({
	Name : 'dong.js',
	Module : 'js/dong/base/config.json'
});
(function() {
	// HTMLScriptElement.prototype.attr = function(n) {
	// return this.getAttribute(n);
	// };
	// HTMLElement.prototype.append = function(n) {
	// this.innerHTML = this.innerHTML + n;
	// return this;
	// };
	String.prototype.trim = function() {
		return this.replace(/(^\s*)|(\s*$)/g, '');
	};
	String.prototype.startsWith = function(_b) {
		var _a = this;
		if (!_a || !_b) {
			return false;
		} else {
			var _len = _b.length;
			if (_len > _a.length) {
				return false;
			} else if (_a.substring(0, _len) == _b) {
				return true;
			} else {
				return false;
			}
		}
	};
	String.prototype.endsWith = function(_b) {
		var _a = this;
		if (!_a || !_b) {
			return false;
		} else {
			var _len = _b.length;
			if (_len > _a.length) {
				return false;
			} else if (_a.substring(_a.length - _len) == _b) {
				return true;
			} else {
				return false;
			}
		}
	};
	Array.prototype.contain = function(e) {
		if (!this || !e) {
			return false;
		}
		var a = this;
		var index = 0;
		for (var i = 0; i < a.length; i++) {
			if (a[i] == e) {
				index++;
				break;
			}
		}
		return index != 0;
	};
})();